using System;
using System.Web;
using System.Collections;


namespace Utils
{

    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public abstract class BaseParameterPasser
        {
        #region Member Variables
        private string url = string.Empty;
        bool askedFlag = false; 
        #endregion

        #region Constructors
        public BaseParameterPasser ( )
            {
            if (HttpContext.Current != null)
                url = HttpContext.Current.Request.Url.ToString ( );
						//debugOk Utils.Debugger.WriteIf ( "My url = HttpContext.Current.Request.Url.ToString ( ); is " + 
								//debugOk HttpContext.Current.Request.Url.ToString () );
            }

        public BaseParameterPasser ( string Url )
            {
            this.url = Url;
            }
        #endregion

        #region Methods
        public virtual void PassParameters ( )
            {
            if (HttpContext.Current != null)
                HttpContext.Current.Response.Redirect ( Url );
            }
        #endregion

        #region Properties
        public string Url
            {
            get
                {
                return url;
                }
            set
                {
                url = value;
                }
            }

        public bool AskedFlag
            {
            get
                {
                return askedFlag;
                }
            set
                {
                askedFlag = value;
                }
            } //eof public bool AskedFlag

        public abstract string this [ string name ]
            {
            get;
            set;
            }

        public abstract ICollection Keys
            {
            get;
            }
        #endregion
        }

	public class UrlParameterPasser : BaseParameterPasser
	{
		#region Member Variables
		private SortedList localQueryString = null;
		#endregion

		#region Constructors
		public UrlParameterPasser() : base() {}		
		public UrlParameterPasser(string Url) : base(Url) {}
		#endregion

		#region Methods


		public override void PassParameters()
		{
			// add parameters, if any exist
			if (localQueryString.Count > 0) 
			{
				// see if we need to add the ?
				if (base.Url.IndexOf("?") == -1)
					base.Url += "?";
				else
					base.Url += "&";

				bool firstOne = true;
				foreach (DictionaryEntry o in localQueryString)
				{
					if (!firstOne)
						base.Url += "&";
					else
						firstOne = false;

					base.Url += string.Concat(HttpContext.Current.Server.UrlEncode(o.Key.ToString()), "=", HttpContext.Current.Server.UrlEncode(o.Value.ToString()));
                } //eof foreach (DictionaryEntry o in localQueryString)
            } //eof if (localQueryString.Count > 0) 

			base.PassParameters();
		} //eof public override void PassParameters();


        /// <summary>
        /// Gets the whole url with all the parameters
        /// </summary>
        /// <returns></returns>
        public string GetWholeURL( )
            {
            //comm -- if I have not been asked yet give answer


                // add parameters, if any exist
                if (localQueryString.Count > 0)
                    {
                    // see if we need to add the ?
                    if (base.Url.IndexOf ( "?" ) == -1)
                        base.Url += "?";
                    else
                        base.Url += "&";

                    bool firstOne = true;
                    foreach (DictionaryEntry o in localQueryString)
                        {		//if there are no parameters return the uril
														if (localQueryString == null)
																return base.Url;

														if (o.Value == null)
																return base.Url;


                        if (!firstOne)
                            base.Url += "&";
                        else
                            firstOne = false;

                        base.Url += string.Concat ( HttpContext.Current.Server.UrlEncode 
                                            ( o.Key.ToString ( ) ), "=", HttpContext.Current.Server.UrlEncode ( o.Value.ToString ( ) ) );
                        } //eof foreach 

                    }
                return base.Url;
            } //eof public override void PassParameters();


        public string GetFullURL ()
        {
        
        string url = this.Url;    
        HttpContext context = HttpContext.Current;
        string server = context.Request.Url.GetComponents ( UriComponents.SchemeAndServer, UriFormat.UriEscaped );
						if (url.Contains(server) )
								return url ;
						else 
								return Combine ( server, url );

        }

        private string Combine ( string baseUrl, string url )
        {
        baseUrl = baseUrl.TrimEnd ( new char [] { '/' } );
        url = url.TrimStart ( new char [ ] { '/' } );
        return baseUrl + "/" + url ;
        
         } //eof private string Combine (string baseUrl , string url ) 
		#endregion

		#region Properties
		public override string this[string name]
		{
			get
			{
				if (localQueryString == null)
				{
					if (HttpContext.Current != null)
						return HttpContext.Current.Request.QueryString[name];
					else
						return null;
				}
				else
					return localQueryString[name].ToString();
			}
			set
			{
				if (localQueryString == null)
					localQueryString = new SortedList();

				// add if it is new, or replace the old value
				if ((localQueryString[name]) == null) 
					localQueryString.Add(name, value);
				else 
					localQueryString[name] = value;
			}
		}

		public override ICollection Keys
		{
			get
			{
				if (localQueryString == null)
				{
					if (HttpContext.Current != null)
						return HttpContext.Current.Request.QueryString.Keys;
					else
						return null;
				}
				else
					return localQueryString.Keys;
			}
		}

		#endregion
	}
} //eof namespace Utils
